{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"request_id\":\"1603384213,b1c1fa99-6c3f-4170-8ed7-38835e5b279c\",\"time_used\":515,\"faces\":[{\"face_token\":\"bebf547668dc1714fa36d9d510433d12\",\"face_rectangle\":{\"top\":160,\"left\":1258,\"width\":114,\"height\":114},\"landmark\":{\"contour_chin\":{\"x\":1298,\"y\":263},\"contour_left1\":{\"x\":1270,\"y\":173},\"contour_left2\":{\"x\":1267,\"y\":183},\"contour_left3\":{\"x\":1265,\"y\":194},\"contour_left4\":{\"x\":1264,\"y\":205},\"contour_left5\":{\"x\":1265,\"y\":216},\"contour_left6\":{\"x\":1268,\"y\":226},\"contour_left7\":{\"x\":1273,\"y\":236},\"contour_left8\":{\"x\":1279,\"y\":246},\"contour_left9\":{\"x\":1287,\"y\":255},\"contour_right1\":{\"x\":1377,\"y\":210},\"contour_right2\":{\"x\":1373,\"y\":221},\"contour_right3\":{\"x\":1368,\"y\":231},\"contour_right4\":{\"x\":1362,\"y\":240},\"contour_right5\":{\"x\":1354,\"y\":249},\"contour_right6\":{\"x\":1344,\"y\":255},\"contour_right7\":{\"x\":1334,\"y\":260},\"contour_right8\":{\"x\":1323,\"y\":263},\"contour_right9\":{\"x\":1311,\"y\":265},\"left_eye_bottom\":{\"x\":1290,\"y\":188},\"left_eye_center\":{\"x\":1293,\"y\":184},\"left_eye_left_corner\":{\"x\":1282,\"y\":178},\"left_eye_lower_left_quarter\":{\"x\":1285,\"y\":184},\"left_eye_lower_right_quarter\":{\"x\":1297,\"y\":190},\"left_eye_pupil\":{\"x\":1294,\"y\":183},\"left_eye_right_corner\":{\"x\":1304,\"y\":191},\"left_eye_top\":{\"x\":1295,\"y\":178},\"left_eye_upper_left_quarter\":{\"x\":1288,\"y\":176},\"left_eye_upper_right_quarter\":{\"x\":1301,\"y\":183},\"left_eyebrow_left_corner\":{\"x\":1279,\"y\":160},\"left_eyebrow_lower_left_quarter\":{\"x\":1289,\"y\":161},\"left_eyebrow_lower_middle\":{\"x\":1297,\"y\":164},\"left_eyebrow_lower_right_quarter\":{\"x\":1305,\"y\":169},\"left_eyebrow_right_corner\":{\"x\":1313,\"y\":174},\"left_eyebrow_upper_left_quarter\":{\"x\":1289,\"y\":157},\"left_eyebrow_upper_middle\":{\"x\":1299,\"y\":160},\"left_eyebrow_upper_right_quarter\":{\"x\":1309,\"y\":165},\"mouth_left_corner\":{\"x\":1289,\"y\":240},\"mouth_lower_lip_bottom\":{\"x\":1304,\"y\":254},\"mouth_lower_lip_left_contour1\":{\"x\":1298,\"y\":243},\"mouth_lower_lip_left_contour2\":{\"x\":1293,\"y\":246},\"mouth_lower_lip_left_contour3\":{\"x\":1297,\"y\":250},\"mouth_lower_lip_right_contour1\":{\"x\":1313,\"y\":248},\"mouth_lower_lip_right_contour2\":{\"x\":1316,\"y\":253},\"mouth_lower_lip_right_contour3\":{\"x\":1310,\"y\":255},\"mouth_lower_lip_top\":{\"x\":1306,\"y\":247},\"mouth_right_corner\":{\"x\":1320,\"y\":250},\"mouth_upper_lip_bottom\":{\"x\":1305,\"y\":242},\"mouth_upper_lip_left_contour1\":{\"x\":1303,\"y\":236},\"mouth_upper_lip_left_contour2\":{\"x\":1295,\"y\":237},\"mouth_upper_lip_left_contour3\":{\"x\":1297,\"y\":240},\"mouth_upper_lip_right_contour1\":{\"x\":1311,\"y\":238},\"mouth_upper_lip_right_contour2\":{\"x\":1316,\"y\":243},\"mouth_upper_lip_right_contour3\":{\"x\":1313,\"y\":245},\"mouth_upper_lip_top\":{\"x\":1307,\"y\":238},\"nose_contour_left1\":{\"x\":1316,\"y\":194},\"nose_contour_left2\":{\"x\":1302,\"y\":212},\"nose_contour_left3\":{\"x\":1302,\"y\":225},\"nose_contour_lower_middle\":{\"x\":1309,\"y\":231},\"nose_contour_right1\":{\"x\":1328,\"y\":196},\"nose_contour_right2\":{\"x\":1324,\"y\":219},\"nose_contour_right3\":{\"x\":1318,\"y\":230},\"nose_left\":{\"x\":1297,\"y\":219},\"nose_right\":{\"x\":1325,\"y\":228},\"nose_tip\":{\"x\":1303,\"y\":221},\"right_eye_bottom\":{\"x\":1350,\"y\":207},\"right_eye_center\":{\"x\":1351,\"y\":202},\"right_eye_left_corner\":{\"x\":1338,\"y\":202},\"right_eye_lower_left_quarter\":{\"x\":1344,\"y\":204},\"right_eye_lower_right_quarter\":{\"x\":1357,\"y\":207},\"right_eye_pupil\":{\"x\":1350,\"y\":201},\"right_eye_right_corner\":{\"x\":1362,\"y\":205},\"right_eye_top\":{\"x\":1352,\"y\":196},\"right_eye_upper_left_quarter\":{\"x\":1344,\"y\":197},\"right_eye_upper_right_quarter\":{\"x\":1359,\"y\":199},\"right_eyebrow_left_corner\":{\"x\":1343,\"y\":184},\"right_eyebrow_lower_left_quarter\":{\"x\":1351,\"y\":185},\"right_eyebrow_lower_middle\":{\"x\":1360,\"y\":186},\"right_eyebrow_lower_right_quarter\":{\"x\":1368,\"y\":188},\"right_eyebrow_right_corner\":{\"x\":1376,\"y\":191},\"right_eyebrow_upper_left_quarter\":{\"x\":1351,\"y\":180},\"right_eyebrow_upper_middle\":{\"x\":1361,\"y\":180},\"right_eyebrow_upper_right_quarter\":{\"x\":1370,\"y\":183}},\"attributes\":{\"gender\":{\"value\":\"Female\"},\"age\":{\"value\":27},\"smile\":{\"value\":27.930,\"threshold\":50.000},\"headpose\":{\"pitch_angle\":9.625363,\"roll_angle\":17.074085,\"yaw_angle\":2.2655764},\"blur\":{\"blurness\":{\"value\":0.375,\"threshold\":50.000},\"motionblur\":{\"value\":0.375,\"threshold\":50.000},\"gaussianblur\":{\"value\":0.375,\"threshold\":50.000}},\"eyestatus\":{\"left_eye_status\":{\"no_glass_eye_open\":99.191,\"no_glass_eye_close\":0.000,\"normal_glass_eye_open\":0.004,\"normal_glass_eye_close\":0.000,\"dark_glasses\":0.005,\"occlusion\":0.801},\"right_eye_status\":{\"no_glass_eye_open\":1.046,\"no_glass_eye_close\":0.000,\"normal_glass_eye_open\":0.000,\"normal_glass_eye_close\":0.000,\"dark_glasses\":0.001,\"occlusion\":98.953}},\"emotion\":{\"anger\":0.000,\"disgust\":0.001,\"fear\":0.000,\"happiness\":0.095,\"neutral\":99.349,\"sadness\":0.554,\"surprise\":0.000},\"facequality\":{\"value\":90.630,\"threshold\":70.100},\"ethnicity\":{\"value\":\"\"},\"beauty\":{\"male_score\":76.620,\"female_score\":77.064},\"mouthstatus\":{\"surgical_mask_or_respirator\":0.000,\"other_occlusion\":100.000,\"close\":0.000,\"open\":0.000},\"eyegaze\":{\"left_eye_gaze\":{\"position_x_coordinate\":0.392,\"position_y_coordinate\":0.352,\"vector_x_component\":0.127,\"vector_y_component\":-0.153,\"vector_z_component\":0.980},\"right_eye_gaze\":{\"position_x_coordinate\":0.649,\"position_y_coordinate\":0.447,\"vector_x_component\":-0.163,\"vector_y_component\":-0.049,\"vector_z_component\":0.985}},\"skinstatus\":{\"health\":69.903,\"stain\":2.228,\"dark_circle\":9.102,\"acne\":2.403},\"glass\":{\"value\":\"None\"}}}],\"image_id\":\"otkvlFGX6CnSVhluUOYalw==\",\"face_num\":1}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "import urllib.request\n",
    "import urllib.error\n",
    "import time\n",
    "\n",
    "http_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "key = \"SsJKglqGQZdoT9kbp7F5td-Gw6JHtn_k\"\n",
    "secret = \"TutG56OCxGDuQjRc_429aQVge1qN9212\"\n",
    "filepath = r\"D:\\pic\\Picture\\1_120518162432_3.jpg\"\n",
    "\n",
    "boundary = '----------%s' % hex(int(time.time() * 1000))\n",
    "data = []\n",
    "data.append('--%s' % boundary)\n",
    "data.append('Content-Disposition: form-data; name=\"%s\"\\r\\n' % 'api_key')\n",
    "data.append(key)\n",
    "data.append('--%s' % boundary)\n",
    "data.append('Content-Disposition: form-data; name=\"%s\"\\r\\n' % 'api_secret')\n",
    "data.append(secret)\n",
    "data.append('--%s' % boundary)\n",
    "fr = open(filepath, 'rb')\n",
    "data.append('Content-Disposition: form-data; name=\"%s\"; filename=\" \"' % 'image_file')\n",
    "data.append('Content-Type: %s\\r\\n' % 'application/octet-stream')\n",
    "data.append(fr.read())\n",
    "fr.close()\n",
    "data.append('--%s' % boundary)\n",
    "data.append('Content-Disposition: form-data; name=\"%s\"\\r\\n' % 'return_landmark')\n",
    "data.append('1')\n",
    "data.append('--%s' % boundary)\n",
    "data.append('Content-Disposition: form-data; name=\"%s\"\\r\\n' % 'return_attributes')\n",
    "data.append(\n",
    "    \"gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus\")\n",
    "data.append('--%s--\\r\\n' % boundary)\n",
    "\n",
    "for i, d in enumerate(data):\n",
    "    if isinstance(d, str):\n",
    "        data[i] = d.encode('utf-8')\n",
    "\n",
    "http_body = b'\\r\\n'.join(data)\n",
    "\n",
    "# build http request\n",
    "req = urllib.request.Request(url=http_url, data=http_body)\n",
    "\n",
    "# header\n",
    "req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)\n",
    "\n",
    "try:\n",
    "    # post data to server\n",
    "    resp = urllib.request.urlopen(req, timeout=5)\n",
    "    # get response\n",
    "    qrcont = resp.read()\n",
    "    # if you want to load as json, you should decode first,\n",
    "    # for example: json.loads(qrount.decode('utf-8'))\n",
    "    print(qrcont.decode('utf-8'))\n",
    "except urllib.error.HTTPError as e:\n",
    "    print(e.read().decode('utf-8'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
